package com.ruoyi.web.controller.lottery;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.lottery.domain.LotAward;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lottery.domain.LotActivity;
import com.ruoyi.lottery.service.ILotActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 抽奖活动Controller
 *
 * @author shier
 * @date 2023-01-27
 */
@RestController
@RequestMapping("/lottery/activity")
public class LotActivityController extends BaseController
{
    @Autowired
    private ILotActivityService lotActivityService;

    /** 某一活动下的抽奖接口 */
    @PostMapping("/lotteryAward")
    public AjaxResult lotteryAward(Long activityId) {

        // TODO: 判断是否登录, 只有登陆了才能查询;否则排除异常

        // 提示信息
        String msg = lotActivityService.lotteryAward(activityId);

        return AjaxResult.success(msg);
    }


    /**
     * 查询抽奖活动列表
     */
    @PreAuthorize("@ss.hasPermi('lottery:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(LotActivity lotActivity)
    {
        startPage();
        List<LotActivity> list = lotActivityService.selectLotActivityList(lotActivity);
        return getDataTable(list);
    }

    /**
     * 导出抽奖活动列表
     */
    @PreAuthorize("@ss.hasPermi('lottery:activity:export')")
    @Log(title = "抽奖活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LotActivity lotActivity)
    {
        List<LotActivity> list = lotActivityService.selectLotActivityList(lotActivity);
        ExcelUtil<LotActivity> util = new ExcelUtil<LotActivity>(LotActivity.class);
        util.exportExcel(response, list, "抽奖活动数据");
    }

    /**
     * 获取抽奖活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('lottery:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(lotActivityService.getById(id));
    }

    /**
     * 新增抽奖活动
     */
    @PreAuthorize("@ss.hasPermi('lottery:activity:add')")
    @Log(title = "抽奖活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LotActivity lotActivity)
    {
        return toAjax(lotActivityService.save(lotActivity));
    }

    /**
     * 修改抽奖活动
     */
    @PreAuthorize("@ss.hasPermi('lottery:activity:edit')")
    @Log(title = "抽奖活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LotActivity lotActivity)
    {
        return toAjax(lotActivityService.updateById(lotActivity));
    }

    /**
     * 删除抽奖活动
     */
    @PreAuthorize("@ss.hasPermi('lottery:activity:remove')")
    @Log(title = "抽奖活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lotActivityService.removeBatchByIds(Arrays.asList(ids)));
    }
}
